package framework.mvc;

import java.util.List;

import org.hibernate.Session;

import framework.config.HibernateUtil;

@SuppressWarnings("rawtypes")
public abstract class GenericDAO<ENTIDADE extends Object> {
	protected Session session;
	protected Class<?> classe;

	public GenericDAO() {
		session = HibernateUtil.getSession();
	}

	public void salvar(Object obj) {
		session.beginTransaction();
		session.saveOrUpdate(obj);
		session.getTransaction().commit();
	}

	public void excluir(Object obj) {
		session.beginTransaction();
		session.delete(obj);
		session.getTransaction().commit();
	}

	public List executarQuery(String sql) {
		return session.createQuery(sql).list();
	}

	public List listar() {
		return executarQuery("from " + classe.getName());
	}

	public Session getSession() {
		return session;
	}

	public Class<?> getClasse() {
		return classe;
	}

	public void setClasse(Class<?> classe) {
		this.classe = classe;
	}

}
